home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Rounding a double
- Date: 1 Apr 1996 19:12:05 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jq5u5INN80u@mayne.ugrad.cs.ubc.ca>
- References: <4jprh2$1li@newsgate.dircon.co.uk>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <4jprh2$1li@newsgate.dircon.co.uk>,
- Peter Smith <pg-smith@dircon.co.uk> wrote:
- >Has anybody got a C function to round a double to a number of decimal
- >places that:
- >a) is readable
- >b) is relatively short
- >c) works !!
- >I had a function but it failed if the number of decimal places was zero,
- >if the double was very small, .. . etc etc.
- >
- >Is there a library of freeware C functions anywhere on the Internet ??
- >
- >Many thanks
- >Pete
- >
- >replies to : pg-smith@dircon.co.uk
- >
-
- double round(double x, int digits)
-
- {
- char buffer[20];
- sprintf(buffer,"%8.8f",x);
-
- buffer >>= (8 - digits);
-
- return atof(buffer);
- }
- --
-
-